package com.example.excise06.controller;

import com.example.excise06.entity.UserEntity;
import com.example.excise06.repository.UserRepository;
import com.example.excise06.security.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/api")
public class HelloController {
    private UserRepository userRepository;

    public HelloController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping("/account")
    public ResponseEntity getAccount() {
        String login = SecurityUtils.getCurrentUserLogin().get();
        Optional<UserEntity> userEntityOptional = userRepository.findByLogin(login);
        if (userEntityOptional.isPresent()) {
            return ResponseEntity.ok(userEntityOptional.get());
        } else {
            return ResponseEntity.badRequest().body("未找到登录用户信息");
        }
    }

    @GetMapping("/")
    public ResponseEntity<String> hello() {
        return ResponseEntity.ok("hello security");
    }
}